#!/usr/bin/perl

use strict;

package main;

use Getopt::Long;
use Lunmgr ':all';
use Data::Dumper;

my %_opt;

GetOptions(\%_opt,
          "-all",
          "-xml",
          "-help|h" => \&usage) or usage();

my @pathes = $_opt{all} ? () : split /,/, join(q{,}, @ARGV);

&lm_print($_opt{xml}, @pathes);

sub usage
{
    print "Usage:\n";
    exit;
}

sub lm_print
{
    my ($xmlopt, @pathes) = @_;

    my @devices = ReadTable("/home/lunmgr/lunmgr.devtable", @pathes);

    print "<?xml version=\"1.0\" ?>\n<lunmgr_conf>\n" if ($xmlopt);

    my $prevwwn;
    foreach my $device (@devices) {
    
        if ($xmlopt) {
            if ($prevwwn ne $device->wwn) {
                print "  </disk_enclosure>\n" if ($prevwwn);
                printf "  <disk_enclosure wwpn=\"%s\">\n", $device->wwn;
                $prevwwn = $device->wwn;
            }

            print $device->ToPrintXML(' ' x 4) . "\n";
        }
        else {
            print $device->ToPrintStr . "\n\n";
        }
 
    }
    print "  </disk_enclosure>\n</lunmgr_conf>\n" if ($xmlopt);
}
